# base image
FROM registry.cn-hangzhou.aliyuncs.com/test_azj/python:3.10-slim-bookworm AS base
#FROM registry2-qingdao.cosmoplat.com/base/python:3.10 AS base
WORKDIR /app/api

# Install Poetry
ENV POETRY_VERSION=1.8.3
RUN pip install --default-timeout=100 -i https://pypi.tuna.tsinghua.edu.cn/simple --no-cache-dir poetry==${POETRY_VERSION}

# Configure Poetry
ENV POETRY_CACHE_DIR=/tmp/poetry_cache
ENV POETRY_NO_INTERACTION=1
ENV POETRY_VIRTUALENVS_IN_PROJECT=true
ENV POETRY_VIRTUALENVS_CREATE=true

FROM base AS packages
#RUN ls -l /etc/apt/sources.list.d
#RUN cat /etc/apt/sources.list.d/debian.sources
#RUN mv /etc/apt/sources.list.d /etc/apt/sources.list
#RUN ls -l /etc/apt/sources.list.d
# 清除已有的源
#RUN nano /etc/apt/sources.list
#RUN apt-key adv --key[server] hkp://keyserver.ubuntu.com:80 --recv-keys 40976EAF437D05B5
#RUN apt-key adv --key[server](https://geek.csdn.net/educolumn/bad8391fc833098e2831fe032051b06?spm=1055.2569.3001.10083) #hkp://keyserver.ubuntu.com:80 --recv-keys 3B4FE6ACC0B21F32
# 添加新的源
RUN touch /etc/apt/sources.list
RUN echo "deb http://mirrors.aliyun.com/debian/ bullseye main non-free contrib\n" >> /etc/apt/sources.list
RUN echo "deb-src http://mirrors.aliyun.com/debian/ bullseye main non-free contrib\n" >> /etc/apt/sources.list
RUN echo "deb http://mirrors.aliyun.com/debian-security/ bullseye-security main non-free contrib\n" >> /etc/apt/sources.list
RUN echo "deb-src http://mirrors.aliyun.com/debian-security/ bullseye-security main non-free contrib" >> /etc/apt/sources.list
RUN cat /etc/apt/sources.list
#RUN sed -i 's/archive.ubuntu.com/mirrors.ustc.edu.cn/g' /etc/apt/sources.list
RUN apt-get update \
    && apt-get install -y --no-install-recommends gcc g++ libc-dev libffi-dev libgmp-dev libmpfr-dev libmpc-dev
#RUN apt-get install -y --no-install-recommends gcc g++ libc-dev libffi-dev libgmp-dev libmpfr-dev libmpc-dev
# Install Python dependencies
COPY pyproject.toml poetry.lock ./
RUN poetry source add --priority=primary mirrors https://mirrors.aliyun.com/pypi/simple
RUN poetry lock
RUN poetry install --sync --no-cache --no-root

# production stage
FROM base AS production

ENV FLASK_APP=app.py
ENV EDITION=SELF_HOSTED
ENV DEPLOY_ENV=PRODUCTION
ENV CONSOLE_API_URL=http://127.0.0.1:5001
ENV CONSOLE_WEB_URL=http://127.0.0.1:3000
ENV SERVICE_API_URL=http://127.0.0.1:5001
ENV APP_WEB_URL=http://127.0.0.1:3000

EXPOSE 5001

# set timezone
ENV TZ=UTC

WORKDIR /app/api
RUN ls -l /var/lib/apt/lists
RUN touch /etc/apt/sources.list
RUN echo "deb http://mirrors.aliyun.com/debian/ bullseye main non-free contrib\n" >> /etc/apt/sources.list
RUN echo "deb-src http://mirrors.aliyun.com/debian/ bullseye main non-free contrib\n" >> /etc/apt/sources.list
RUN echo "deb http://mirrors.aliyun.com/debian-security/ bullseye-security main non-free contrib\n" >> /etc/apt/sources.list
RUN echo "deb-src http://mirrors.aliyun.com/debian-security/ bullseye-security main non-free contrib" >> /etc/apt/sources.list
RUN echo "deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free non-free-firmware" >> /etc/apt/sources.list
RUN echo "deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware" >> /etc/apt/sources.list
RUN echo "deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-backports main contrib non-free non-free-firmware" >> /etc/apt/sources.list
RUN echo "deb https://mirrors.tuna.tsinghua.edu.cn/debian-security bookworm-security main contrib non-free non-free-firmware" >> /etc/apt/sources.list
RUN echo "deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm InRelease contrib non-free non-free-firmware" >> /etc/apt/sources.list
RUN cat /etc/apt/sources.list

RUN apt-get update \
    && apt-get install -y --no-install-recommends curl wget vim nodejs ffmpeg libgmp-dev libmpfr-dev libmpc-dev \
    && apt-get autoremove \
    && rm -rf /var/lib/apt/lists/*

# Copy Python environment and packages
ENV VIRTUAL_ENV=/app/api/.venv
COPY --from=packages ${VIRTUAL_ENV} ${VIRTUAL_ENV}
ENV PATH="${VIRTUAL_ENV}/bin:${PATH}"

# Copy source code
COPY . /app/api/

# Copy entrypoint
COPY docker/entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh


ARG COMMIT_SHA
ENV COMMIT_SHA=${COMMIT_SHA}

ENTRYPOINT ["/bin/bash", "/entrypoint.sh"]
